/*
 * @lc app=leetcode.cn id=232 lang=javascript
 *
 * [232] 用栈实现队列
 */

// @lc code=start

class Stack {
    constructor() {
        this.items = [];
    }

    push(x) {
        this.items.push(x);
    }

    pop() {
        return this.items.pop();
    }

    peek() {
        return this.items[this.size() - 1];
    }

    size() {
        return this.items.length;
    }

    empty() {
        return this.size() === 0;
    }

    clear() {
        this.items = [];
    }
}

var MyQueue = function() {
    this.stack1 = new Stack();
    this.stack2 = new Stack();
};

/**
 * @param {number} x
 * @return {void}
 */
MyQueue.prototype.push = function(x) {
    this.stack1.push(x);
};

/**
 * @return {number}
 */
MyQueue.prototype.pop = function() {
    if (this.stack1.empty()) {
        return undefined;
    }

    while(this.stack1.size() > 1) {
        this.stack2.push(this.stack1.pop());
    }
    const res = this.stack1.pop();
    while(!this.stack2.empty()) {
        this.stack1.push(this.stack2.pop())
    }
    return res;
};

/**
 * @return {number}
 */
MyQueue.prototype.peek = function() {
    if (this.stack1.empty()) {
        return undefined;
    }

    while(this.stack1.size() > 1) {
        this.stack2.push(this.stack1.pop());
    }
    const res = this.stack1.pop();
    this.stack1.push(res);
    while(!this.stack2.empty()) {
        this.stack1.push(this.stack2.pop())
    }
    return res;
};

/**
 * @return {boolean}
 */
MyQueue.prototype.empty = function() {
    return this.stack1.empty();
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * var obj = new MyQueue()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.peek()
 * var param_4 = obj.empty()
 */
// @lc code=end

